#include "AddBookDialog.h"

#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/datectrl.h> // 确保包含日期选择器头文件
#include <wx/spinctrl.h> // 确保包含旋转控制器头文件
#include "../book_info.h"

BEGIN_EVENT_TABLE(AddBookDialog, wxDialog)
    EVT_BUTTON(wxID_OK, AddBookDialog::OnOK)
    EVT_BUTTON(wxID_CANCEL, AddBookDialog::OnCancel)
END_EVENT_TABLE()

AddBookDialog::AddBookDialog(wxWindow* parent)
    : wxDialog(parent, wxID_ANY, wxT("添加图书信息"), wxDefaultPosition, wxSize(400, 300)) {

    wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);

    wxFlexGridSizer* fgs = new wxFlexGridSizer(2, 5, 9);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("ID")), 0, wxALIGN_CENTER_VERTICAL);
    m_idCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_idCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("标题")), 0, wxALIGN_CENTER_VERTICAL);
    m_titleCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_titleCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("作者")), 0, wxALIGN_CENTER_VERTICAL);
    m_authorCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_authorCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("出版社")), 0, wxALIGN_CENTER_VERTICAL);
    m_publisherCtrl = new wxTextCtrl(this, wxID_ANY);
    fgs->Add(m_publisherCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("出版日期")), 0, wxALIGN_CENTER_VERTICAL);
    m_publishDateCtrl = new wxDatePickerCtrl(this, wxID_ANY, wxDefaultDateTime, wxDefaultPosition, wxDefaultSize, wxDP_DROPDOWN | wxDP_SHOWCENTURY);
    fgs->Add(m_publishDateCtrl, 1, wxEXPAND);

    fgs->Add(new wxStaticText(this, wxID_ANY, wxT("数量")), 0, wxALIGN_CENTER_VERTICAL);
    m_quantityCtrl = new wxSpinCtrl(this, wxID_ANY, wxT("0"), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 1000, 0);
    fgs->Add(m_quantityCtrl, 1, wxEXPAND);

    vbox->Add(fgs, 1, wxALL | wxEXPAND, 10);

    wxBoxSizer* btnSizer = new wxBoxSizer(wxHORIZONTAL);
    btnSizer->Add(new wxButton(this, wxID_OK, wxT("确定")), 0, wxALL, 5);
    btnSizer->Add(new wxButton(this, wxID_CANCEL, wxT("取消")), 0, wxALL, 5);

    vbox->Add(btnSizer, 0, wxALIGN_CENTER);

    SetSizer(vbox);
    Centre();
}

BookInfo AddBookDialog::GetBookInfo() const {
    return BookInfo(
        m_idCtrl->GetValue().ToStdString(),
        m_titleCtrl->GetValue().ToStdString(),
        m_authorCtrl->GetValue().ToStdString(),
        m_publisherCtrl->GetValue().ToStdString(),
        m_publishDateCtrl->GetValue().GetTicks(),
        m_quantityCtrl->GetValue()
    );
}

void AddBookDialog::OnOK(wxCommandEvent& event) {
    EndModal(wxID_OK);
}

void AddBookDialog::OnCancel(wxCommandEvent& event) {
    EndModal(wxID_CANCEL);
}